using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.ServiceModel;
using WFExtensions;

namespace EAppointments.BMS.Hosts.WindowsServiceHost
{
    public partial class WindowsServiceHost : ServiceBase
    {
        private static ServiceHost _appointmentServiceHost;
        private static ServiceHost _directoryServiceHost;
        private static ServiceHost _providerServiceHost;
        
        public WindowsServiceHost()
        {
            CreateServiceHosts();
            InitializeComponent();
        }

        /// <summary>
        /// The OnStart method is called when the Windows Service starts.   When this occurs
        /// we will start the service hosts for our WCF services.
        /// </summary>
        /// <param name="args"></param>
        protected override void OnStart(string[] args)
        {
            try
            {
                //Open the Appointment Service Host
                _appointmentServiceHost.Open();
                OutputServiceInformation(_appointmentServiceHost);

                //Open the Directory Service Host
                _directoryServiceHost.Open();
                OutputServiceInformation(_directoryServiceHost);

                //Open the Provider Service Host
                _providerServiceHost.Open();
                OutputServiceInformation(_providerServiceHost);

            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
                throw;
            }
        }

        /// <summary>
        /// The OnStop method is called when the service is stopped.   When this occurs
        /// we will stop the WCF service hosts.   
        /// </summary>
        protected override void OnStop()
        {
            _appointmentServiceHost.Close();
            _directoryServiceHost.Close();
            _providerServiceHost.Close();

        }

        private static void CreateServiceHosts()
        {
            try
            {
                //Create the host for the WCF services
                _appointmentServiceHost = new WFServiceHost(typeof(EAppointments.BMS.Services.AppointmentService));
                _directoryServiceHost = new ServiceHost(typeof(EAppointments.BMS.Services.DirectoryService));
                _providerServiceHost = new ServiceHost(typeof(EAppointments.BMS.Services.ProviderService));
                
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
                throw;
            }
        }


        /// <summary>
        /// This method will write out the information about a service host to the console.  
        /// </summary>
        /// <param name="hostOrderService"></param>
        private static void OutputServiceInformation(ServiceHostBase serviceHost)
        {
            string endpointAddresses = GetEndpointAddresses(serviceHost.Description);
            
            // Un-comment this is you want to write to the EventLog.
            //EventLog.WriteEntry("EAppointments Services Host", string.Format("The {0} service is running at:{1}{2}", serviceHost.Description.Name, Environment.NewLine, endpointAddresses));
        }

        /// <summary>
        /// This method will return a formatted string of endpoints for a given ServiceDescription.
        /// </summary>
        /// <param name="description"></param>
        /// <returns></returns>
        private static string GetEndpointAddresses(System.ServiceModel.Description.ServiceDescription description)
        {
            // Create a new string builder for managing the output string
            StringBuilder stringBuilder = new StringBuilder();

            // Enumerate through each of the ServiceEndPoints and append them to the string.
            foreach (System.ServiceModel.Description.ServiceEndpoint serviceEndPoint in description.Endpoints)
            {
                stringBuilder.AppendFormat("\n\t{0}", serviceEndPoint.Address.ToString());
            }

            // Return the formatted EndPoints
            return stringBuilder.ToString();
        }
    }
}
